package com.shuaiche.yeguan.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.shuaiche.yeguan.SCApplication;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/***
 * 设备帮助类
 *
 */
public class DeviceHelper {

    /**
     * 获取设备 SDK版本号
     */
    public static int getSDKVersionInt() {
        return Build.VERSION.SDK_INT;
    }

    /**
     * 获取系统版本
     *
     * @return
     */
    public static String getOSVersion() {
        return Build.VERSION.RELEASE;
    }



    /**
     * 获取屏幕分辨率
     *
     * @param activity
     * @return
     */
    public static int[] getScreenDispaly(Activity activity) {
        WindowManager winManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        Display display = winManager.getDefaultDisplay();
        int result[] = {display.getWidth(), display.getHeight()};
        return result;
    }


    public static int getScreenWidthPx(Context context){
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;// 屏幕宽
    }

    public static int getScreenWidthPx(){
        return Resources.getSystem().getDisplayMetrics().widthPixels;// 屏幕宽
    }
    public static int getScreenHeightPx(){
        return Resources.getSystem().getDisplayMetrics().heightPixels;// 屏幕高
    }

    /**
     * 获取app版本名称
     *
     * @return
     */
    public static String getAppVersion() {
        String version = "";
        PackageManager packageManager = SCApplication.getApplication().getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo(SCApplication.getApplication().getPackageName(), 0);
            version = info.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        return version;
    }

    /**
     * 获取app版本code
     *
     * @return
     */
    public static int getAppVersionCode() {
        int version = 0;
        PackageManager packageManager = SCApplication.getApplication().getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo(SCApplication.getApplication().getPackageName(), 0);
            version = info.versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        return version;
    }

    /**
     * 获取ip
     *
     * @return
     */
    public static String getNetworkIP() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return null;
    }

    public static int getStatusBarHeight(Context context) {
        int result = 89;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
